{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bbfbe10",
   "metadata": {},
   "outputs": [],
   "source": [
    "在编写程序的过程中，有某一功能的代码块出现多次，为了提高编写的效率以及代码的重复使用，所以把具有独立功能的代码块组织为一个小模块，这就是函数\n",
    "### 1.创建（定义）一个函数\n",
    "方法:\n",
    "```\n",
    "def 函数名():\n",
    "    代码块\n",
    "    pass\n",
    "```\n",
    "例：\n",
    "```\n",
    "def inf():\n",
    "    print('147')\n",
    "    pass\n",
    "inf()\n",
    "```\n",
    "运行结果为打印出147\n",
    "\n",
    "### 2.函数的参数\n",
    "参数:其实就是函数为了实现某项特定的功能，进而为了得到实现功能所需要的数据，得到外部数据\n",
    "例：\n",
    "```\n",
    "def sum(a,b)  #形式参数：只是意义上的一种参数，在定义的时候是不占内存地址的\n",
    "    sum=a+b\n",
    "    print(sum)\n",
    "    pass\n",
    "sum(20,15)  #实际参数：是实际占用内存地址的\n",
    "```\n",
    "运行结果为打印出35\n",
    "\n",
    "还可以为函数添加**默认参数**，例：\n",
    "```\n",
    "def sum(a=20,b=30) #默认参数，在调用函数时若不输入参数则会使用默认参数\n",
    "    print('默认参数相加为%d'%(a+b))\n",
    "sum()\n",
    "sum(10)\n",
    "```\n",
    "运行结果为打印出默认参数相加为50和默认参数相加为40\n",
    "### 3.可变参数\n",
    "当**参数个数不确定**时使用args，例：\n",
    "```\n",
    "def sum(*args):\n",
    "    for i in args:\n",
    "        result+=i\n",
    "    print('result=%d'%(result))\n",
    "sum(1,2,3)\n",
    "```\n",
    "运行结果为打印出“resul=6”\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
